package com.ferry.controller;

import com.ferry.domain.WallType;
import com.ferry.service.WallTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
public class TypeController {

    @Autowired
    private WallTypeService wallTypeService;

    @GetMapping("/type")
    public String type(Model model) {

        List<WallType> all = wallTypeService.getAll();
        model.addAttribute("types",all);

        return "type";
    }

    @PostMapping("/type/add")
    public String add(String name,Model model) {

        if (!StringUtils.isEmpty(name)) {
            boolean add = wallTypeService.add(name);
            if (add) {
                model.addAttribute("msg","添加成功");
            } else {
                model.addAttribute("msg","添加失败");
            }
        } else {
            model.addAttribute("msg","类型名称不能为空");
        }
        List<WallType> all = wallTypeService.getAll();
        model.addAttribute("types",all);
        return "type";
    }

    @GetMapping("/type/delete/{id}")
    public String delete(@PathVariable Long id,Model model) {
        if (id != null) {
            boolean delete = wallTypeService.delete(id);
            if (delete) {
                model.addAttribute("msg","删除成功");
            } else {
                model.addAttribute("msg","删除失败");
            }
        } else {
            model.addAttribute("msg","类型不存在");
        }
        List<WallType> all = wallTypeService.getAll();
        model.addAttribute("types",all);
        return "type";
    }
}
